package com.xiaofengstu.mcams.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Path;
import static java.nio.file.FileSystems.*;

/**
 * @Author FengZeng
 * @Date 2022-05-20 13:41
 * @Description TODO
 */
@Component
public class QRCodeGenerator {

  public void generateQRCodeImage(String text, String filePath)throws WriterException, IOException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 350, 350);
    Path path = getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
  }

}
